/*
 * xfaidx.h
 *
 *  Created on: Aug 8, 2011
 *      Author: Pierre Lindenbaum PhD
 *              plindenbaum@yahoo.fr
 *              http://plindenbaum.blogspot.com
 *              
 */

#ifndef XFAIDX_H_
#define XFAIDX_H_
#include <string>
#include <stdint.h>
#include <map>
#include <vector>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "xstring.h"
#include "typedfile.h"

/* it is the structure created by samtools faidx */
typedef struct
	 {
	 int32_t line_len, line_blen;
	 int64_t len;
	 uint64_t offset;
	 }faidx1_t;

typedef int tid_t;

/* extended with the name and tid*/
typedef struct faidx2_t:public faidx1_t
	 {
	 char* name;
	 tid_t tid;
	 }faidx2_t,*FaidxPtr;


class IndexedGenome
    {
    private:
		 unsigned long _total_size;
		 /* maps a chromosome to the samtools faidx index */
		 std::map<std::string,FaidxPtr> name2index;
		 TypedFile<char>* file;
		/** tid to FaidxPtr */
		std::vector<FaidxPtr> tid2index;
    public:
		IndexedGenome(const char* fasta);
		virtual ~IndexedGenome();
		char at(const FaidxPtr faidx,int64_t index);
		/** number of fragments */
		int size() const;
		/** total size of the genome in pb */
		unsigned long genome_size() const;
		const FaidxPtr getFaidByTid(tid_t tid) const;
		const FaidxPtr getFaidByName(const char* s) const;
    };

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/


class ChromosomeString:public CharSequence
    {
    private:
	IndexedGenome* _genome;
	FaidxPtr _faidx;
    public:
	ChromosomeString(IndexedGenome* genome,FaidxPtr faidx);
	virtual ~ChromosomeString();
	virtual const char* name() const;
	virtual size_type size() const;
	virtual char at(size_type idx) const;
    };

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

#endif /* XFAIDX_H_ */
